package com.stupro.cjlu.config;

import com.stupro.cjlu.manager.LoginStatusManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    private Session session;
    @Autowired
    private LoginStatusManager loginStatusManager;


    public LoginInterceptor() {    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Boolean flag = loginStatusManager.checkLoginState(session);
        if(!flag){
            response.setStatus(401);
            response.sendError(401,"没有登录");
        }
        return flag;
    }
}
